package me.zhengjie.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.agvSend.AgvSendService;
import me.zhengjie.pojo.dto.AgvTaskSubmitDto;
import me.zhengjie.pojo.dto.CarrierUnbindDto;
import me.zhengjie.pojo.dto.DeliveryDto;
import me.zhengjie.pojo.dto.TargetRoute;
import me.zhengjie.pojo.entity.WcsInboundMission;
import me.zhengjie.pojo.entity.WcsOutboundMission;
import me.zhengjie.service.DeliveryService;
import me.zhengjie.sqlservice.WcsInboundMissionService;
import me.zhengjie.sqlservice.WcsOutboundMissionService;
import me.zhengjie.util.state.TaskStatus;
import me.zhengjie.util.state.TaskType;
import me.zhengjie.utility.IdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class DeliveryServiceImpl implements DeliveryService {

    private static final Logger outbound = LoggerFactory.getLogger("wcsOutbound");

    private final AgvSendService agvSendService;

    private final IdWorker idWorker;

    private final WcsOutboundMissionService wcsOutboundMissionService;

    private final WcsInboundMissionService wcsInboundMissionService;

    @Override
    @Transactional
    public boolean deliveryTask(DeliveryDto oneFloorOutboundDto) {
        Integer deliveryType = oneFloorOutboundDto.getDeliveryType();
        String floor = oneFloorOutboundDto.getFloor();
        String source = oneFloorOutboundDto.getSource();
        String destination = oneFloorOutboundDto.getDestination();
        Integer priority = oneFloorOutboundDto.getPriority();
        String taskType = oneFloorOutboundDto.getTaskType();
        String materIalCode = oneFloorOutboundDto.getMaterIalCode();
        String wmsTaskId = oneFloorOutboundDto.getWmsTaskId();
        if ("1".equals(floor)) {
            if (deliveryType == 2) {
                //发送任务给AGV，发送成功后创建出库任务
//            String floor = oneFloorOutboundDto.getFloor();

                String outboundCode = String.valueOf(idWorker.nextId());
                AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
                //todo 任务类型，根据现场情况来定
                agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_COMMON.getMessage());
                ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
                TargetRoute targetRoute = new TargetRoute();
                TargetRoute targetRoutes = new TargetRoute();
                //添加起点
                targetRoute.setSeq(0);
                targetRoute.setType(TaskType.STORAGE.getMessage());
                targetRoute.setCode(source);
                //添加终点
                targetRoutes.setSeq(1);
                targetRoutes.setType(TaskType.SITE.getMessage());
                targetRoutes.setCode(destination);
                targetRouteList.add(targetRoute);
                targetRouteList.add(targetRoutes);
                agvTaskSubmitDto.setTargetRoute(targetRouteList);
                agvTaskSubmitDto.setRobotTaskCode(outboundCode);
                agvTaskSubmitDto.setInitPriority(priority);
                String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
                if (taskCode != null) {
                    //创建出库任务
                    WcsOutboundMission wcsOutboundMission = new WcsOutboundMission();
                    wcsOutboundMission.setOutboundCode(taskCode);
                    wcsOutboundMission.setMissionStatus(TaskStatus.task_send.getStatus());
                    wcsOutboundMission.setContainerCode(oneFloorOutboundDto.getContainerCode());
                    wcsOutboundMission.setSource(source);
                    wcsOutboundMission.setDestination(destination);
                    wcsOutboundMission.setCreateTime(LocalDateTime.now());
                    wcsOutboundMission.setUpdateTime(LocalDateTime.now());
                    wcsOutboundMission.setPriority(priority);
                    wcsOutboundMission.setTaskType(taskType);
                    wcsOutboundMission.setContainerType(TaskType.PALLET.getStatus());
                    wcsOutboundMission.setIsNest(0);
                    wcsOutboundMission.setWmsTaskId(wmsTaskId);
                    boolean b = wcsOutboundMissionService.insertOutbound(wcsOutboundMission);
                    if (!b) {
                        outbound.error("数据库异常，添加出库任务失败");
                        throw new RuntimeException("数据库异常，添加出库任务失败");
                    }
                    return true;
                }
                return false;
            }
        } else if ("2".equals(floor)) {
            if (deliveryType == 2) {
                //出库，添加到出库任务
                //创建出库任务
                WcsOutboundMission wcsOutboundMission = new WcsOutboundMission();
                wcsOutboundMission.setOutboundCode(String.valueOf(idWorker.nextId()));
                wcsOutboundMission.setMissionStatus(TaskStatus.Task_create.getStatus());
                wcsOutboundMission.setContainerCode(oneFloorOutboundDto.getContainerCode());
                wcsOutboundMission.setSource(source);
                wcsOutboundMission.setDestination(destination);
                wcsOutboundMission.setCreateTime(LocalDateTime.now());
                wcsOutboundMission.setUpdateTime(LocalDateTime.now());
                wcsOutboundMission.setContainerType(TaskType.WORKBIN.getStatus());
                wcsOutboundMission.setPriority(priority);
                wcsOutboundMission.setTaskType(taskType);
                wcsOutboundMission.setIsNest(0);
                wcsOutboundMission.setWmsTaskId(wmsTaskId);
                boolean b = wcsOutboundMissionService.insertOutbound(wcsOutboundMission);
                if (!b) {
                    outbound.error("数据库异常，添加出库任务失败");
                    throw new RuntimeException("数据库异常，添加出库任务失败");
                }
            }
        }
        if (deliveryType !=2) {
            //储位绑定栈板
            CarrierUnbindDto carrierUnbindDto = new CarrierUnbindDto();
            carrierUnbindDto.setSlotCategory(TaskType.SITE.getMessage());
            carrierUnbindDto.setSlotCode(source);
            carrierUnbindDto.setCarrierCategory("PALLET");
            carrierUnbindDto.setCarrierType("1");
            carrierUnbindDto.setInvoke(TaskType.BIND.getMessage());
            agvSendService.carrierUnbind(carrierUnbindDto);

            String outboundCode = String.valueOf(idWorker.nextId());
            AgvTaskSubmitDto agvTaskSubmitDto = new AgvTaskSubmitDto();
            agvTaskSubmitDto.setTaskType(TaskType.PF_FMR_COMMON.getMessage());
            ArrayList<TargetRoute> targetRouteList = new ArrayList<>();
            TargetRoute targetRoute = new TargetRoute();
            TargetRoute targetRoutes = new TargetRoute();
            //添加起点
            targetRoute.setSeq(0);
            targetRoute.setType(TaskType.SITE.getMessage());
            targetRoute.setCode(source);
            //添加终点
            targetRoutes.setSeq(1);
            targetRoutes.setType(TaskType.STORAGE.getMessage());
            targetRoutes.setCode(destination);
            targetRouteList.add(targetRoute);
            targetRouteList.add(targetRoutes);
            agvTaskSubmitDto.setTargetRoute(targetRouteList);
            agvTaskSubmitDto.setRobotTaskCode(outboundCode);
            agvTaskSubmitDto.setInitPriority(priority);
            String taskCode = agvSendService.agvTaskSubmit(agvTaskSubmitDto);
            if (taskCode != null) {
                //创建出库任务
                WcsInboundMission wcsInboundMission = new WcsInboundMission();
                wcsInboundMission.setInboundCode(taskCode);
                wcsInboundMission.setMissionStatus(TaskStatus.task_send.getStatus());
                wcsInboundMission.setContainerCode(oneFloorOutboundDto.getContainerCode());
                wcsInboundMission.setSource(source);
                wcsInboundMission.setDestination(destination);
                wcsInboundMission.setCreateTime(LocalDateTime.now());
                wcsInboundMission.setUpdateTime(LocalDateTime.now());
                wcsInboundMission.setPriority(priority);
                switch (deliveryType){
                    case 1:
                        wcsInboundMission.setInboundType(TaskType.inbound_ordinary.getStatus());
                        break;
                    case 3:
                        wcsInboundMission.setInboundType(TaskType.circle_library_stocktaking.getStatus());
                        break;
                    case 4:
                        wcsInboundMission.setInboundType(TaskType.circle_library_sorting.getStatus());
                        break;
                    case 5:
                        wcsInboundMission.setInboundType(TaskType.circle_library_mould_assembling.getStatus());
                        break;
                    case 6:
                        wcsInboundMission.setInboundType(TaskType.FREEZE_AND_RETURN_TO_STORAGE.getStatus());
                        break;
                }
                wcsInboundMission.setTaskType(taskType);
                if (Objects.equals(floor, "1")){
                    wcsInboundMission.setContainerType(TaskType.PALLET.getStatus());
                }else  if (Objects.equals(floor, "2")){
                    wcsInboundMission.setContainerType(TaskType.WORKBIN.getStatus());
                }else  if (Objects.equals(floor, "3")){
                    wcsInboundMission.setContainerType(TaskType.THREE_WORKBIN.getStatus());
                }
                if (deliveryType != 1){
                    wcsInboundMission.setIsNest(1);
                }else {
                    wcsInboundMission.setIsNest(0);
                }
                wcsInboundMission.setIsPlc(0);
                wcsInboundMission.setMaterialCode(materIalCode);
                wcsInboundMission.setWmsTaskId(wmsTaskId);
                boolean b = wcsInboundMissionService.insertInbound(wcsInboundMission);
                if (!b) {
                    outbound.error("数据库异常，添加出库任务失败");
                    throw new RuntimeException("数据库异常，添加出库任务失败");
                }
                return true;
            }
        }
        return false;
    }
}
